package com.example.pceb.controller;

import com.example.pceb.entity.SchoolEnterpriseCooperation;
import com.example.pceb.entity.CooperationApplication;
import com.example.pceb.result.Result;
import com.example.pceb.service.CooperationService;
import com.example.pceb.service.CooperationApplicationService;
import com.example.pceb.entity.SchoolAdmin;
import com.example.pceb.entity.School;
import com.example.pceb.service.SchoolAdminService;
import com.example.pceb.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.example.pceb.VO.ProjectProgressVO;
import com.example.pceb.VO.ProjectPhaseUpdateRequest;


@RestController
@RequestMapping({"/api/cooperation", "/cooperation"})
//@CrossOrigin(origins = "*")
public class CooperationController {
    @Autowired
    private CooperationService cooperationService;
    @Autowired
    private CooperationApplicationService applicationService;
    @Autowired
    private SchoolAdminService schoolAdminService;
    @Autowired
    private SchoolService schoolService;

    @PostMapping("/publish")
    public Result<String> publish(@RequestBody SchoolEnterpriseCooperation cooperation) {
        // 学校端自动补全school_name
        if ("学校".equals(cooperation.getPublisherType())) {
            SchoolAdmin admin = schoolAdminService.getByUsername(cooperation.getPublisherUsername());
            if (admin != null && admin.getSchoolId() != null) {
                School school = schoolService.getById(admin.getSchoolId());
                if (school != null) {
                    cooperation.setSchoolName(school.getSchoolName());
                }
            }
        }
        
        boolean success = cooperationService.saveCooperation(cooperation);
        if (success) {
            return Result.success("发布成功");
        } else {
            return Result.error("发布失败");
        }
    }

    @GetMapping("/list")
    public Result<List<SchoolEnterpriseCooperation>> listCooperationProjects(@RequestParam String userType) {
        String publisherType = "school".equals(userType) ? "企业" : "学校";
        String projectStatus = "未立项";
        List<SchoolEnterpriseCooperation> list = cooperationService.listByPublisherType(publisherType, projectStatus);
        return Result.success(list);
    }

    @GetMapping("/detail")
    public Result<SchoolEnterpriseCooperation> getCooperationDetail(@RequestParam Long id) {
        SchoolEnterpriseCooperation detail = cooperationService.getById(id);
        return Result.success(detail);
    }
    
    // 获取学校管理员对应的学校名称
    @GetMapping("/get-school-name")
    public Result<String> getSchoolName(@RequestParam String username) {
        SchoolAdmin admin = schoolAdminService.getByUsername(username);
        if (admin == null || admin.getSchoolId() == null) {
            return Result.error("未找到学校管理员或学校信息");
        }
        
        School school = schoolService.getById(admin.getSchoolId());
        if (school == null) {
            return Result.error("未找到学校信息");
        }
        
        return Result.success(school.getSchoolName());
    }
    
    // 获取我的申请列表
    @GetMapping("/my-applications")
    public Result<List<CooperationApplication>> getMyApplications(@RequestParam String username, @RequestParam String userType) {
        List<CooperationApplication> applications = applicationService.getMyApplications(username, userType);
        return Result.success(applications);
    }
    
    // 获取我的项目列表
    @GetMapping("/my-projects")
    public Result<List<SchoolEnterpriseCooperation>> getMyProjects(@RequestParam String username, @RequestParam String userType) {
        List<SchoolEnterpriseCooperation> projects = cooperationService.getMyProjects(username, userType);
        return Result.success(projects);
    }
    
    // 获取学校管理员项目（状态不为"未立项"）
    @GetMapping("/school-admin-pending-projects")
    public Result<List<SchoolEnterpriseCooperation>> getSchoolAdminPendingProjects(@RequestParam String username) {
        List<SchoolEnterpriseCooperation> projects = cooperationService.getSchoolAdminPendingProjects(username);
        return Result.success(projects);
    }
    
    // 获取企业管理员项目（状态不为"未立项"）
    @GetMapping("/enterprise-admin-pending-projects")
    public Result<List<SchoolEnterpriseCooperation>> getEnterpriseAdminPendingProjects(@RequestParam String username) {
        List<SchoolEnterpriseCooperation> projects = cooperationService.getEnterpriseAdminPendingProjects(username);
        return Result.success(projects);
    }
    
    // 获取项目的申请列表
    @GetMapping("/project-applications")
    public Result<List<CooperationApplication>> getProjectApplications(@RequestParam Long projectId) {
        List<CooperationApplication> applications = applicationService.getProjectApplications(projectId);
        return Result.success(applications);
    }
    
    // 删除项目
    @PostMapping("/delete-project")
    public Result<String> deleteProject(@RequestBody SchoolEnterpriseCooperation cooperation) {
        boolean success = cooperationService.deleteProject(cooperation.getId(), cooperation.getPublisherUsername());
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
    
    @GetMapping("/project-progress")
    public Result<List<ProjectProgressVO>> getProjectProgress(@RequestParam Long projectId) {
        List<ProjectProgressVO> progress = cooperationService.getProjectProgress(projectId);
        return Result.success(progress);
    }
    
    // 更新项目阶段信息
    @PostMapping("/update-phase")
    public Result<String> updateProjectPhase(@RequestBody ProjectPhaseUpdateRequest request) {
        try {
            boolean success = cooperationService.updateProjectPhase(request);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    

}